<?php

class User extends CActiveRecord {
    const STATUS_NORMAL = 1;
    const STATUS_DELETED = 2;
    const STATUS_BLOCKED = 3;
    const STATUS_UNCHECKED = 4;

    /**
     * The followings are the available columns in table 'User':
     * @var integer $id
     * @var integer $language_id
     * @var integer $status
     * @var integer $action_ts
     * @var string $nickname
     * @var string $email
     * @var string $password
     * @var string $first_name
     * @var string $last_name
     * @var integer $birthdate
     * @var integer $timezone_ofs
     * @var string $gender
     * @var integer $reg_date
     * @var string $signature
     * @var string $home_page_url
     * @var string $avatar
     * @var string $short_description
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'User';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('nickname','length','max'=>64),
            array('email','length','max'=>64),
            array('password','length','max'=>32),
            array('first_name','length','max'=>64),
            array('last_name','length','max'=>64),
            array('signature','length','max'=>32),
            array('home_page_url','length','max'=>255),
            array('avatar','length','max'=>255),
            array('nickname, email, password, first_name, last_name, birthdate, timezone_ofs, reg_date', 'required'),
            array('status, birthdate, timezone_ofs, reg_date, action_ts', 'numerical', 'integerOnly'=>true),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
        'authItems' => array(self::MANY_MANY, 'AuthItem', 'AuthAssignment(itemname, userid)'),
        'language' => array(self::BELONGS_TO, 'Language', 'language_id'),
        'userGroups' => array(self::MANY_MANY, 'UserGroup', 'UserGroup_has_User(UserGroup_id, User_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'language_id' => 'Language',
            'status' => 'Status',
            'action_ts'=>'Action timestamp',
            'nickname' => 'Nickname',
            'email' => 'Email',
            'password' => 'Password',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'birthdate' => 'Birthdate',
            'timezone_ofs' => 'Timezone Ofs',
            'gender' => 'Gender',
            'reg_date' => 'Reg Date',
            'signature' => 'Signature',
            'home_page_url' => 'Home Page Url',
            'avatar' => 'Avatar',
        );
    }

    protected function beforeValidate($scenario){
        if($this->getIsNewRecord()){
            $this->reg_date = time();
            $this->status = USER::STATUS_UNCHECKED;
            $this->signature = md5(uniqid(microtime(true), true));
            $this->password = md5($this->password);
        }
        $this->action_ts = time();
        return true;
    }

    public function save($runValidation=true, $attributes=null){
        try{
            return parent::save($runValidation, $attributes);
        }
        catch(CDbException $e){
            return false;
        }
    }

    public function markAsDeleted(){
        if(!$this->getIsNewRecord()){
            $this->status = User::STATUS_DELETED;
            $this->update();
        }
    }

    public function markAsBlocked(){
        if(!$this->getIsNewRecord()){
            $this->status = User::STATUS_BLOCKED;
            $this->update();
        }
    }

    public function markAsNormal(){
        if(!$this->getIsNewRecord()){
            $this->status = User::STATUS_NORMAL;
            $this->update();
        }
    }
}